<template>
	<view class="mian-container">
		<view class="tab-wrap">
			<view class="tab-list">
				<view :class="'tab-item ' + (index === activeIndex ? 'active' : '')" @tap.stop.prevent="changeItem"
					:data-id="item.id" :data-index="index" v-for="(item, index) in typeList" :key="index">
					{{ item.name }}

					<view v-if="index === activeIndex" class="active-line"
						:style="'background:' + platformConfig.baseColor"></view>
				</view>
			</view>
		</view>
		<view class="message-list" v-if="typeList[activeIndex] && typeList[activeIndex].messageList.length > 0">
			<navigator class="message-item" :url="'/pages/message/messageDetail/messageDetail?id=' + item.id"
				v-for="(item, index) in typeList[activeIndex].messageList" :key="index" @click="linkTo(item.messageTitle)">
				<view class="message-left">
					<view class="message-content">{{ item.messageTitle }}</view>
					<view class="message-bottom">
						<view class="message-type">{{ item.typeName }}</view>
						<view class="message-time">{{ item.time }}</view>
					</view>
				</view>

				<image mode="aspectFit" class="message-img" :src="util.attachedUrl(item.image, baseurl)" />
			</navigator>
		</view>
		<view v-if="!typeList[activeIndex] || typeList[activeIndex].messageList.length == 0" class="empty-container">
			<image class="empty-img" src="/static/image/home_ic_empty@2x.png"></image>
			<view>暂无相关记录</view>
		</view>

		<view class="viewMission">
			<block v-if="viewMission">
				<image src="/static/image/view-unfinished@2x.png" style="width: 100rpx; height: 140rpx"></image>
				<text class="endTimer">浏览{{ timeCount }}秒可得奖励</text>
			</block>
			<image class="viewMission-complete" v-if="hasComplete" src="/static/image/view-complete2@2x.png"
				@tap="receivePrice"></image>
		</view>
	</view>
</template>
<script module="util" lang="wxs" src="../../../utils/util.wxs"></script>
<script>
	// pages/message/messageCenter/messageCenter.js
	const app = getApp();
	import {
		baseImgUrl,
	} from '../../../utils/constant.js'
	const utils = require('../../../utils/util.js');
	import {
		http
	} from '@/utils/commonRequest.js'; // 局部引入

	export default {
		data() {
			return {
				baseurl: baseImgUrl,
				platformConfig: app.globalData.platformConfig,
				typeList: [],
				activeIndex: 0,
				viewMission: false,
				hasComplete: false,
				dicType: '',
				dictId: '',
				taskId: '',
				timeCount: '10',
				timeOut: '',
				share: null,
				loading: '',
				messageList: []
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function(options) {
			let that = this;
			this.platformConfig = app.globalData.platformConfig

			//登录之后才查询订单列表，减少请求
			this.$checkLogin().then((res) => {
				this.getMessageType();

				if (options.dictId) {
					that.viewMission = options.share ? false : true
					that.dicType = options.dicType
					that.dictId = options.dictId
					that.taskId = options.taskId
					that.share = options.share
				}
			});
		},
		onShow() {
			let that = this;
			this.$checkLogin()
				.then((res) => {
					if (that.dictId && !this.share) {
						that.timeOutStart();
					}
				})
				.catch((err) => {
					console.log(err);
					that.loading = -1
				});
		},
		/** 页面隐藏 */
		onHide: function() {
			clearInterval(this.timeOut);
		},
		/** 页面关闭 */
		onUnload: function() {
			clearInterval(this.timeOut);
		},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {
			//页面下拉刷新时，将分页查询参数初始化
			let list = this.typeList;
			let index = this.activeIndex;
			console.log(list);
			list[index].messageList = [];
			list[index].haveMore = true;
			list[index].params.pageNum = 1;
			this.typeList = list
			this.isRefresh = true; //标记为该次请求为刷新列表，为true时请求结束后调用wx.stopPullDownRefresh()

			this.getMessageList();
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			let index = this.activeIndex;

			if (!this.typeList[index].haveMore) {
				return;
			} else {
				this.getMessageList();
			}
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function(options) {
			let that = this;

			if (that.dicType == undefined) {
				return;
			} else {
				that.taskAddLog();
				return {
					title: '分享测试',
					path: '/pages/message/messageCenter/messageCenter'
				};
			}
		},
		/** 分享 */
		onShareTimeline: function() {
			let that = this;
			console.log(that.dicType, that.dictId);

			if (that.dicType == undefined) {
				return;
			} else {
				that.taskAddLog();
				return {
					title: '分享测试',
					path: '/pages/message/messageCenter/messageCenter'
				};
			}
		},
		methods: {
			linkTo: function(name){
				this.$zhuge.track('资讯列表-点击公告',{
					'公告标题': name
				})
			},
			changeItem: function(e) {
				let index = e.currentTarget.dataset.index;

				if (index == this.activeIndex) {
					return;
				}

				this.activeIndex = index
				this.getMessageList();
			},

			getMessageType: function() {
				var that = this;
				http.get('message/typeList').then((res) => {
					if ('200' == res.data.code) {
						if (res.data.typeList.length > 0) {
							let list = [];
							res.data.typeList.unshift({
								id: '',
								name: '全部'
							});
							res.data.typeList.map((item) => {
								list.push(
									Object.assign({}, item, {
										messageList: [],
										haveMore: true,
										params: {
											pageSize: 5,
											pageNum: 1,
											typeId: item.id
										}
									})
								);
							});
							that.typeList = list
							that.getMessageList();
						}
					}
				});
			},

			getMessageList: function() {
				let that = this;
				let index = that.activeIndex;
				let list = that.typeList;

				if (!list[index].haveMore) {
					console.log('index:%d没有更多了', index);
					return;
				}

				utils.showLoading('加载中');
				http.get('message/messageListByPage', {
						params: list[index].params
					})
					.then((res) => {
						utils.hideLoading();

						if (res.data.code == '200') {
							list[index].haveMore = res.data.haveMore;

							if (res.data.list.length > 0) {
								res.data.list.forEach((element) => {
									element.time = element.createTime.split(' ')[0];
								});
							}

							list[index].messageList = [...list[index].messageList, ...res.data.list];
							list[index].params.pageNum++;
							that.typeList = list
						} else {
							utils.message(res.msg);
						}

						if (that.isRefresh) {
							uni.stopPullDownRefresh();
							that.isRefresh = false;
						}
					})
					.catch((err) => {
						utils.hideLoading();

						if (that.isRefresh) {
							uni.stopPullDownRefresh();
							that.isRefresh = false;
						}
					});
			},

			timeOutStart: function() {
				let that = this;
				that.timeOut = setInterval(function() {
					let count = that.timeCount - 1;
					that.timeCount = count

					if (that.timeCount == 0) {
						that.viewMission = false
						that.hasComplete = true
						app.globalData.taskLog(that.dicType, that.dictId, that.taskId);
						clearInterval(that.timeOut);
					}
				}, 1000);
			},

			receivePrice: function() {
				uni.navigateBack({
					delta: 1
				});
			},

			taskAddLog: function() {
				let that = this;
				let type = that.dicType;
				let id = that.dictId;
				let taskId = that.taskId;
				app.globalData.taskLog(type, id, taskId, 'share');
			}
		}
	};
</script>
<style>
	::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
		display: none;
	}

	.mian-container {
		padding-top: 150rpx;
	}

	.tab-wrap {
		padding: 0 32rpx;
		background-color: #fff;
		height: 118rpx;
		width: 100%;
		box-sizing: border-box;
		position: fixed;
		top: 0;
		z-index: 1;
		box-shadow: 0 14rpx 34rpx 6rpx rgba(182, 178, 199, 0.2);
	}

	.tab-list {
		padding: 0 30rpx;
		box-sizing: border-box;
		width: 100%;
		height: 100%;
		overflow-x: scroll;
		display: flex;
		align-items: center;
		white-space: nowrap;
		-webkit-overflow-scrolling: touch;
	}

	.tab-item {
		margin-right: 64rpx;
		line-height: 44rpx;
		font-size: 32rpx;
		color: #666;
		position: relative;
	}

	.active {
		color: #000;
		font-size: 36rpx;
		font-weight: bold;
		line-height: 50rpx;
		position: relative;
	}

	.active-line {
		position: absolute;
		width: 72rpx;
		height: 8rpx;
		bottom: -16rpx;
		left: 50%;
		transform: translateX(-50%);
		background: #ff6464;
		border-radius: 4rpx;
	}

	.message-list {
		padding: 0 30rpx;
	}

	.message-item {
		margin-bottom: 30rpx;
		display: flex;
		box-shadow: 0 14rpx 34rpx 6rpx rgba(182, 178, 199, 0.2);
		background: #fff;
		padding: 30rpx 24rpx 24rpx;
		border-radius: 16rpx;
	}

	.message-content {
		font-size: 32rpx;
		color: #000;
		line-height: 44rpx;
		width: 370rpx;
		margin-bottom: 16rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 3;
		height: 132rpx;
		font-weight: bold;
	}

	.message-bottom {
		display: flex;
		justify-content: space-between;
	}

	.message-type {
		max-width: 200rpx;
		font-size: 24rpx;
		color: #ff6e42;
		border: 1px solid #ffd7cb;
		border-radius: 8rpx;
		padding: 8rpx 12rpx;
		line-height: 24rpx;
		background: rgba(255, 110, 66, 0.06);
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.message-time {
		color: #ccc;
		font-size: 24rpx;
		line-height: 40rpx;
	}

	.message-img {
		width: 240rpx;
		height: 180rpx;
		margin-left: 32rpx;
	}

	.empty-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: 26rpx;
		color: #999;
		padding-top: 200rpx;
	}

	.empty-img {
		width: 644rpx;
		height: 310rpx;
	}

	.viewMission {
		position: fixed;
		right: 24rpx;
		top: 50%;
		transform: translateY(-50%);
		z-index: 999;
		/* zoom: .6; */
	}

	.viewMission-complete {
		width: 100rpx;
		height: 140rpx;
		/* zoom: 0.6; */
	}

	.endTimer {
		display: inline-block;
		width: 54rpx;
		height: 56rpx;
		/* line-height: 20rpx; */
		font-size: 12rpx;
		font-weight: 500;
		color: #fff;
		position: absolute;
		top: 70rpx;
		left: 24rpx;
		text-align: center;
		padding-top: 4rpx;
	}
</style>
